package third.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "CreateCookieServlet", value = "/createcookie")
public class CreateCookieServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1创建cookie
        Cookie cookie = new Cookie("username", "zhangsan");
        //2设置属性
        //设置有效期
        cookie.setMaxAge(60 * 60 * 24);  //-1 cookie只保存在内存中，浏览器关闭消失,  0 删除cookie   60 second有效期
        //设置有效路径
        cookie.setPath("/");
        //设置httponly
        cookie.setHttpOnly(true);
        //3cookie添加到response对象
        response.addCookie(cookie);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
